<script setup>
import {
  Edit,
  Delete,
  Close
} from '@element-plus/icons-vue'


import {ref} from 'vue'


const contentTypes = ref([])


const versionSeasons = ref([])
const iconColors = ref([

  {
    "id": 1,
    "name": "灰色",
  },
  {
    "id": 2,
    "name": "青色",
  },
  {
    "id": 3,
    "name": "绿色",
  },
  {
    "id": 4,
    "name": "黄绿",
  },
  {
    "id": 5,
    "name": "黄",
  },
  {
    "id": 6,
    "name": "橘黄",
  },
  {
    "id": 7,
    "name": "橘",
  },
  {
    "id": 8,
    "name": "橘红",
  },
  {
    "id": 9,
    "name": "红",
  },
  {
    "id": 10,
    "name": "紫红",
  },
  {
    "id": 11,
    "name": "紫",
  }
])

const resourceTypes = ref([

  {
    "id": 1,
    "name": "头像",
  },
  {
    "id": 2,
    "name": "头像框",
  },
  {
    "id": 3,
    "name": "表情",
  },
  {
    "id": 4,
    "name": "表情包",
  },
  {
    "id": 5,
    "name": "卡片背景",
  }
])


//用户搜索时选中的分类id
const contentType = ref('')

//用户搜索时选中的发布状态
const state = ref('')


const rewardSeasonId = ref('')

const mileStones = ref([])


//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(15)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  mileStoneList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  mileStoneList()
}


import {versionSeasonListService} from '@/api/versionSeason.js'

import {
  mileStoneListService,
  mileStoneAddService,
  mileStoneUpdateService,
  mileStoneDelService
} from '@/api/mileStone.js'


import {Plus} from '@element-plus/icons-vue'
import {QuillEditor} from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const mileStoneModel = ref({})


//导入token
import {useTokenStore} from '@/stores/token.js';

const tokenStore = useTokenStore();


//上传成功的回调函数
const uploadNormalSuccess = (result) => {
  mileStoneModel.value.normalImgUrl = result.data.fileUrl;
  mileStoneModel.value.normalFileName = result.data.fileName;
}

const uploadMemberSuccess = (result) => {
  mileStoneModel.value.memberImgUrl = result.data.fileUrl;
  mileStoneModel.value.memberFileName = result.data.fileName;
}


//添加文章
import {ElMessage} from 'element-plus'

const addSeason = async (clickState) => {
  //把发布状态赋值给数据模型
  mileStoneModel.value.state = clickState;

  //调用接口
  let result = await mileStoneAddService(mileStoneModel.value);

  ElMessage.success(result.msg ? result.msg : '添加成功');

  //让抽屉消失
  visibleDrawer.value = false;

  //刷新当前列表
  mileStoneList()
}

//编辑分类
const updateSeason = async () => {
  //调用接口
  let result = await mileStoneUpdateService(mileStoneModel.value);

  ElMessage.success(result.msg ? result.msg : '修改成功')

  //调用获取所有分类的函数
  mileStoneList();

  //隐藏弹窗
  visibleDrawer.value = false;
}


//获取文章列表数据
const mileStoneList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    rewardSeasonId: rewardSeasonId.value ? rewardSeasonId.value : null

  }
  let result = await mileStoneListService(params);

  total.value = result.data.total;
  mileStones.value = result.data.records;

}


const versionSeasonList = async () => {
  let params = {
    type: 2
  }

  let result = await versionSeasonListService(params);
  versionSeasons.value = result.data;


}
mileStoneList();
versionSeasonList();


//删除分类
import {ElMessageBox} from 'element-plus'
import {interestDeleteInterestPublishService} from "@/api/interestPublishRecordData.js";


//定义变量,控制标题的展示
const title = ref('')
const showMileStone = (row) => {
  visibleDrawer.value = true, title.value = '编辑里程碑'
  mileStoneModel.value.id= row.id;
  mileStoneModel.value.level =row.level;
  mileStoneModel.value.rewardSeasonId =row.rewardSeasonId;
  mileStoneModel.value.iconColor =row.iconColor;
  mileStoneModel.value.pointNum =row.pointNum;
  if(row.normalReward){
    mileStoneModel.value.normalFileType=row.normalReward.resourceType;
    mileStoneModel.value.normalFileName=row.normalReward.resourceName;
    mileStoneModel.value.normalImgUrl=row.normalReward.resourceUrl;
  }
  if(row.memberReward){
    mileStoneModel.value.memberFileType=row.memberReward.resourceType;
    mileStoneModel.value.memberFileName=row.memberReward.resourceName;
    mileStoneModel.value.memberImgUrl=row.memberReward.resourceUrl;
  }
}

//清空模型的数据
const clearData = () => {

  mileStoneModel.value.id= '';
  mileStoneModel.value.level ='';
  mileStoneModel.value.rewardSeasonId ='';
  mileStoneModel.value.iconColor ='';
  mileStoneModel.value.pointNum ='';
  mileStoneModel.value.normalFileType='';
  mileStoneModel.value.normalImgUrl='';
  mileStoneModel.value.memberFileType='';
  mileStoneModel.value.memberImgUrl='';
}
const deleteMileStone = (row) => {
  //提示用户  确认框

  ElMessageBox.confirm(
      '你确认要删除该里程碑信息吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        let params={
          id:row.id
        }
        //调用接口
        let result = await mileStoneDelService(params);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        mileStoneList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了删除',
        })
      })
}

const delNormalImg = () => {
  mileStoneModel.value.normalImgUrl = '';
  mileStoneModel.value.normalFileName = '';
}

const delMemberImg = () => {
  mileStoneModel.value.memberImgUrl = '';
  mileStoneModel.value.memberFileName = '';
}


</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>里程碑</span>
        <div class="extra">
          <el-button type="primary" @click="visibleDrawer = true; title = '添加里程碑'; clearData()">添加里程碑
          </el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="赛事：">
        <el-select placeholder="请选择" v-model="rewardSeasonId">
          <el-option v-for="c in versionSeasons" :key="c.id" :label="c.name" :value="c.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="mileStoneList">搜索</el-button>
        <el-button @click="rewardSeasonId = ''">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="mileStones" border style="width: 100%">

      <el-table-column label="版本" prop="name"></el-table-column>
      <el-table-column label="等级" prop="level"></el-table-column>
      <el-table-column label="数值" prop="pointNum"></el-table-column>
      <el-table-column label="图标颜色" prop="iconColorName"></el-table-column>
      <el-table-column prop="id" v-if="show" ></el-table-column>
      <el-table-column label="普通奖励类型">
        <template v-slot="scope">
          <span>{{ scope.row.normalReward ? scope.row.normalReward.resourceTypeName : '' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="普通奖励">
        <template v-slot="scope">
          <img v-if="scope.row.normalReward" :src="scope.row.normalReward.resourceUrl" width="40" height="40" class="head_pic" />
        </template>
      </el-table-column>

      <el-table-column label="会员奖励类型">
        <template v-slot="scope">
          <span>{{ scope.row.memberReward ? scope.row.memberReward.resourceTypeName : '' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="会员奖励">
        <template v-slot="scope">
          <img v-if="scope.row.memberReward" :src="scope.row.memberReward.resourceUrl" width="40" height="40" class="head_pic" />

        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="200px" prop="createTime"></el-table-column>

      <el-table-column label="操作" width="150px">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showMileStone(row)"></el-button>
          <el-button  :icon="Delete" circle plain type="danger" @click="deleteMileStone(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>


    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total"
                   @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end"/>


    <!-- 抽屉 -->
    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
      <el-form :model="mileStoneModel" label-width="100px">
        <el-form-item label="赛事">
          <el-select placeholder="请选择" v-model="mileStoneModel.rewardSeasonId">
            <el-option v-for="c in versionSeasons" :key="c.id" :label="c.name" :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图标颜色">
          <el-select placeholder="请选择" v-model="mileStoneModel.iconColor">
            <el-option v-for="c in iconColors" :key="c.id" :label="c.name" :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="等级">
          <el-input-number v-model="mileStoneModel.level" :min="1"/>
        </el-form-item>

        <el-form-item label="数值">
          <el-input v-model="mileStoneModel.pointNum"/>
        </el-form-item>
        <el-form-item label="普通奖励类型">
          <el-select placeholder="请选择" v-model="mileStoneModel.normalFileType">
            <el-option v-for="c in resourceTypes" :key="c.id" :label="c.name" :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="普通奖励">
          <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false"
                     action="/api/admin/upload/uploadFile/v2" name="file"
                     :headers="{ 'Authorization': tokenStore.token }"
                     :on-success="uploadNormalSuccess">
            <img v-if="mileStoneModel.normalImgUrl" :src="mileStoneModel.normalImgUrl" class="avatar"/>
            <el-icon v-else class="avatar-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
          <span v-if="show">{{ mileStoneModel.normalFileName }}</span>
          <el-icon v-if="mileStoneModel.normalImgUrl" @click="delNormalImg()">
            <Close/>
          </el-icon>
        </el-form-item>

        <el-form-item label="会员奖励类型">
          <el-select placeholder="请选择" v-model="mileStoneModel.memberFileType">
            <el-option v-for="c in resourceTypes" :key="c.id" :label="c.name" :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员奖励">
          <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false"
                     action="/api/admin/upload/uploadFile/v2" name="file"
                     :headers="{ 'Authorization': tokenStore.token }"
                     :on-success="uploadMemberSuccess">
            <img v-if="mileStoneModel.memberImgUrl" :src="mileStoneModel.memberImgUrl" class="avatar"/>
            <el-icon v-else class="avatar-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
          <span v-if="show">{{ mileStoneModel.memberFileName }}</span>
          <el-icon v-if="mileStoneModel.memberImgUrl" @click="delMemberImg()">
            <Close/>
          </el-icon>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="title == '添加里程碑' ? addSeason() : updateSeason()">保存</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>


  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}


/* 抽屉样式 */
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>